/*
 * iShopPlace Project.
 */
package ishopplace.view.bean;

import ishopplace.model.service.CatalogService;
import ishopplace.model.service.CustomerService;
import ishopplace.model.service.OrderService;
import ishopplace.view.servicelocator.ServiceLocator;
import ishopplace.view.util.FacesUtils;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * The implementation of <code>ServiceLocator</code>.
 * <p>
 * This class is managed by the JSF managed bean facility,
 * and is set with application scope.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 * @see ServiceLocator
 */
public class ServiceLocatorBean implements ServiceLocator {
	//the catalog service bean name
	private static final String CATALOG_SERVICE_BEAN_NAME = "catalogService";
	
	//the customer service bean name
	private static final String CUSTOMER_SERVICE_BEAN_NAME = "customerService";
	
	//the order service bean name
	private static final String ORDER_SERVICE_BEAN_NAME = "orderService";
	
	//the logger for this class
	private Log logger = LogFactory.getLog(this.getClass());
	
	//the Spring application context
	private ApplicationContext appContext;
	
	//the cached catalog service
	private CatalogService catalogService;
	
	//the cached customer service
	private CustomerService customerService;
	
	//the cached order service
	private OrderService orderService;
	
	/**
	 * Constructor.
	 * <p>
	 * The following steps being performed:
	 * <ul>
	 * <li>retrieve Spring application context from servlet context.
	 * <li>look up <code>CatalogService</code> from Spring application context.
	 * <li>look up <code>CustomerService</code> from Spring applicatino context.
	 * <li>look up <code>OrderService</code> from Spring applicatino context.
	 * </ul>
	 */
	public ServiceLocatorBean() {
		ServletContext context = FacesUtils.getServletContext();
		this.appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		this.catalogService = (CatalogService)this.lookupService(CATALOG_SERVICE_BEAN_NAME);
		this.customerService = (CustomerService)this.lookupService(CUSTOMER_SERVICE_BEAN_NAME);
		this.orderService = (OrderService)this.lookupService(ORDER_SERVICE_BEAN_NAME);
		this.logger.info("Service locator bean is initialized");
	}
	
	/**
	 * Get the <code>CatalogService</code>.
	 * 
	 * @return the catalog service
	 */
	public CatalogService getCatalogService() {
		return this.catalogService;
	}
	
	/**
	 * Get the <code>CustomerService</code>.
	 * 
	 * @return the customer service
	 */
	public CustomerService getCustomerService() {
		return this.customerService;
	}
	
	/**
	 * Get the <code>OrderService</code>.
	 * 
	 * @return the order service
	 */
	public OrderService getOrderService() {
		return this.orderService;
	}
	
	/**
	 * Lookup a service based on the service bean name.
	 * 
	 * @param serviceBeanName the service bean name
	 * @return the service bean
	 */
	public Object lookupService(String serviceBeanName) {
		return appContext.getBean(serviceBeanName);
	}
}
